package com.lifeverse.entity;

import com.lifeverse.entity.enums.PermissionType;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

/**
 * 权限实体
 */
@Entity
@Table(name = "permissions", indexes = {
    @Index(name = "idx_permission_code", columnList = "permission_code", unique = true),
    @Index(name = "idx_permission_type", columnList = "permission_type"),
    @Index(name = "idx_permission_parent", columnList = "parent_id")
})
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Permission extends BaseEntity {
    
    /**
     * 权限代码
     */
    @Column(name = "permission_code", nullable = false, unique = true, length = 100)
    private String permissionCode;
    
    /**
     * 权限名称
     */
    @Column(name = "permission_name", nullable = false, length = 100)
    private String permissionName;
    
    /**
     * 权限描述
     */
    @Column(name = "description", length = 500)
    private String description;
    
    /**
     * 权限类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "permission_type", nullable = false, length = 20)
    private PermissionType permissionType;
    
    /**
     * 资源路径
     */
    @Column(name = "resource_path", length = 200)
    private String resourcePath;
    
    /**
     * HTTP方法
     */
    @Column(name = "http_method", length = 10)
    private String httpMethod;
    
    /**
     * 父权限ID
     */
    @Column(name = "parent_id")
    private Long parentId;
    
    /**
     * 权限级别
     */
    @Column(name = "permission_level")
    private Integer permissionLevel;
    
    /**
     * 是否启用
     */
    @Column(name = "enabled", nullable = false)
    private Boolean enabled;
    
    /**
     * 是否为系统权限
     */
    @Column(name = "is_system", nullable = false)
    private Boolean isSystem;
    
    /**
     * 排序号
     */
    @Column(name = "sort_order")
    private Integer sortOrder;
    
    /**
     * 图标
     */
    @Column(name = "icon", length = 50)
    private String icon;
    
    /**
     * 创建权限的静态方法
     */
    public static Permission create(String permissionCode, String permissionName, 
                                  PermissionType permissionType, String description) {
        return Permission.builder()
                .permissionCode(permissionCode)
                .permissionName(permissionName)
                .permissionType(permissionType)
                .description(description)
                .enabled(true)
                .isSystem(false)
                .permissionLevel(1)
                .sortOrder(0)
                .build();
    }
    
    /**
     * 创建系统权限的静态方法
     */
    public static Permission createSystemPermission(String permissionCode, String permissionName,
                                                  PermissionType permissionType, String description) {
        return Permission.builder()
                .permissionCode(permissionCode)
                .permissionName(permissionName)
                .permissionType(permissionType)
                .description(description)
                .enabled(true)
                .isSystem(true)
                .permissionLevel(1)
                .sortOrder(0)
                .build();
    }
    
    /**
     * 创建API权限的静态方法
     */
    public static Permission createApiPermission(String permissionCode, String permissionName,
                                               String resourcePath, String httpMethod, String description) {
        return Permission.builder()
                .permissionCode(permissionCode)
                .permissionName(permissionName)
                .permissionType(PermissionType.API)
                .resourcePath(resourcePath)
                .httpMethod(httpMethod)
                .description(description)
                .enabled(true)
                .isSystem(false)
                .permissionLevel(1)
                .sortOrder(0)
                .build();
    }
    
    /**
     * 创建菜单权限的静态方法
     */
    public static Permission createMenuPermission(String permissionCode, String permissionName,
                                                String resourcePath, String icon, String description) {
        return Permission.builder()
                .permissionCode(permissionCode)
                .permissionName(permissionName)
                .permissionType(PermissionType.MENU)
                .resourcePath(resourcePath)
                .icon(icon)
                .description(description)
                .enabled(true)
                .isSystem(false)
                .permissionLevel(1)
                .sortOrder(0)
                .build();
    }
}